import axios, { AxiosError } from "axios";
import { showLoading, hideLoading } from "./loading";
import { Result } from "@/types/api";
import { message } from "./AntdGlobal";
import storage from "./storage";
import env from "@/config";
const instance = axios.create({
    timeout: 8000,
    withCredentials: true,
    timeoutErrorMessage: "请求超时，请稍后再试",
    headers: {
        icode: "D40C45E6760A33D1",
    },
});

//请求拦截器
instance.interceptors.request.use(
    config => {
        if (config.showLoading) showLoading();
        const token = storage.get("token");
        if (token) {
            config.headers.Authorization = "Bearer " + token;
        }
        if (env.mock) {
            config.baseURL = env.mockApi;
        } else {
            config.baseURL = env.baseApi;
        }
        return {
            ...config,
        };
    },
    (error: AxiosError) => {
        return Promise.reject(error);
    }
);

//响应拦截器

instance.interceptors.response.use(
    response => {
        const data: Result = response.data;
        hideLoading();
        if (data.code === 500001) {
            message.error(data.msg);
            storage.remove("token");
            location.href = "/login?callback=" + encodeURIComponent(location.href);
        } else if (data.code !== 0) {
            if (response.config.showError === false) {
                return Promise.resolve(data);
            }
            message.error(data.msg);
            return Promise.reject(data);
        }
        return data.data;
    },
    error => {
        hideLoading();
        message.error(error.message);
        return Promise.reject(error);
    }
);

interface IConfig {
    showLoading: boolean;
    showError: boolean;
}

export default {
    get<T>(url: string, params?: object, options: IConfig = { showLoading: true, showError: true }): Promise<T> {
        return instance.get(url, { params, ...options });
    },
    //简写方式
    // get:function(url: string, params: any) {
    //     return instance.get(url, { params })
    // },
    post<T>(url: string, params?: object, options: IConfig = { showLoading: true, showError: true }): Promise<T> {
        return instance.post(url, params, options);
    },
};
